using System;
using Science.Mathematics;
using L=Science.Physics.GeneralPhysics;
using M = Science.Mathematics.Calculus;

namespace Serway.Chapter23
{
	/// <summary>
	/// Example08: The Electric Field of a Uniform Ring of Charge
	/// A ring of radius a carries a uniformly distributed positive
	/// total charge Q. Calculate the electric field due to the ring
	/// at a point P lying a distance x from its center along the 
	/// central axis perpendicular to the plane of the ring.
	/// E_x = k_e x Q /(x^2 + a^2)^{3/2}	
	/// </summary>
	public class Example08
	{
		public Example08()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		double ke = L.Constant.CoulombConstant;
		double Q = 10.0;
		double a = 2.0;
		double x = 3.0;
		double y = 0.00000000001;
		double z = 0.00000000001;
		L.Position r = new L.Position();
		L.Position rp = new L.Position();
		public void Compute()
		{
			rp.X = x;
			rp.Y = y;
			rp.Z = z;

			Function.DoubleToDouble Ex
				= new Function.DoubleToDouble(funcx);
			M.Integration1D projx = new M.Integration1D(Ex);
			projx.From = 0.0;
			projx.To = 2.0*Math.PI;
			projx.Compute();

			Function.DoubleToDouble Ey
				= new Function.DoubleToDouble(funcy);
			M.Integration1D projy = new M.Integration1D(Ey);
			projy.From = 0.0;
			projy.To = 2.0*Math.PI;
			projy.Compute();

			Function.DoubleToDouble Ez
				= new Function.DoubleToDouble(funcz);
			M.Integration1D projz = new M.Integration1D(Ez);
			projz.From = 0.0;
			projz.To = 2.0*Math.PI;
			projz.Compute();

			result+=Convert.ToString(projx.Result)+"  i + ";  
			result+=Convert.ToString(projy.Result)+"  j + ";  
			result+=Convert.ToString(projz.Result)+"  k \r\n";  
			result+=Convert.ToString(ke*Q*x/Math.Pow(x*x+a*a,1.5));  
		}
		private double funcx(double theta)
		{
			r.X = 0.0;
			r.Y = a*Math.Cos(theta);
			r.Z = a*Math.Sin(theta);
			
			L.ElectricCharge q = new L.ElectricCharge();
			q.C = Q/2.0/Math.PI;
			L.ElectricField E = new L.ElectricField(q,r,rp);
			return E.X;
		}
		private double funcy(double theta)
		{
			r.X = 0.0;
			r.Y = a*Math.Cos(theta);
			r.Z = a*Math.Sin(theta);
		
			L.ElectricCharge q = new L.ElectricCharge();
			q.C = Q/2.0/Math.PI;
			L.ElectricField E = new L.ElectricField(q,r,rp);
			return E.Y;
		}
		private double funcz(double theta)
		{
			r.X = 0.0;
			r.Y = a*Math.Cos(theta);
			r.Z = a*Math.Sin(theta);
		
			L.ElectricCharge q = new L.ElectricCharge();
			q.C = Q/2.0/Math.PI;
			L.ElectricField E = new L.ElectricField(q,r,rp);
			return E.Z;
		}
	}
}
